const app = getApp();
let date = new Date();
let currentYear = date.getFullYear(),currentMonth = date.getMonth(),currentDay = date.getDate()
Page({
  data: {
    loading:true,
    dropdown:3,
    store_ids:{store_id:0,store_chain_id:0},
    minDate: new Date(currentYear,currentMonth-2,1).getTime(),
    maxDate: new Date(currentYear,currentMonth+1,currentDay).getTime(),
    amount:{order:0,inc:0,dec:0},
    calendar:{},
    store: {},
    list: [],
    page: 0,
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    app.isLogin(()=>{
      this.getStore();
    });
  },
  /**
   * 获取店铺信息
   */
  getStore: function () {
    app.api().Get("api/v1/smartbc/store/storeInfo",(result) => {
      this.setData({
        store:result.data
      })
      this.getList();
    })
  },
  //下拉刷新
  onPullDownRefresh: function () {
    wx.showNavigationBarLoading();
    setTimeout(()=>{
      wx.hideNavigationBarLoading();
      wx.stopPullDownRefresh();
    },1500);
    this.setData({
      loading: true,
      page:0,
      list:[]
    });
    this.getList();
  },
  //上拉加载更多
  onReachBottom: function () {
    this.setData({
      loading: true,
    }); 
    this.getList();
  },
  //读取账单
  onBill() {
    this.setData({
      loading: true,
      page: 0,
      list: [],
    });
    this.getList();
  },
  /**
   * 账单
   */
  getList: function () {
    if (this.data.loading) {
      var param = {
        page:this.data.page + 1,
        store_id:this.data.store_ids.store_id,
        store_chain_id:this.data.store_ids.store_chain_id,
        fixed_date:this.data.dropdown,
        times:JSON.stringify(this.data.calendar)
      }
      app.api().Get("api/v1/smartbc/store/bill",param,(result) => {
        if (result.code == 200) {
          var list = this.data.list;
          for (let i in result.data.bill) {
            list.push(result.data.bill[i]);
          }
          this.setData({
            list: list,
            page: param.page,
          });
          if(param.page == 1){
            this.setData({
              amount:result.data.amount
            });
          }
        }
      })
      this.setData({
        loading: false,
      });
    }
  },
  //门店查询
  onStore(e){
    this.data.store_ids = e.currentTarget.dataset
    this.selectComponent('#storeSearch').toggle();
    this.onBill();
    this.setData({
      calendar:{}
    })
  },
  //定期查询
  onDates(e){
    this.setData({
      dropdown:e.detail,
      calendar:{}
    })
    this.onBill();
  },
  //日期区间搜索
  onClose(){
    this.setData({
      showCalendar:!this.data.showCalendar
    })
  },
  onCalendar(e){
    this.onClose()
    this.data.times = e.currentTarget.dataset.name
  },
  onConfirm(e){
    date = new Date(e.detail);
    var getdata  = ("0"+(date.getDate())).slice(-2),getmonth = parseInt(("0"+(date.getMonth())).slice(-2)) + 1
    var times    = `${date.getFullYear()}-${getmonth}-${getdata}`;
    var calendar = this.data.calendar;calendar[this.data.times] = times
    this.setData({
      showCalendar: false,
      calendar:calendar
    });
  },
  onSearch(){
    var calendar  = this.data.calendar;
    if(app.util().isNull(calendar.startime) || app.util().isNull(calendar.endtime)){
        app.wxAlert('请输入查询日期区间')
    }
    this.setData({dropdown:3});
    this.selectComponent('#dataSearch').toggle();
    this.onBill();
  },
  //滚动
  onPageScroll(res){
    let scrollTop = res.scrollTop;
    this.setData({
      showbar: 10 <= scrollTop ?true :false
    })
  }
})